package com.example.a_java.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.a_java.common.Result;
import com.example.a_java.entity.SysDept;
import com.example.a_java.service.SysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Tag(name = "部门管理", description = "部门管理相关接口")
@RestController
@RequestMapping("/system/dept")
@SecurityRequirement(name = "bearer-key")
public class SysDeptController {

    @Autowired
    private SysDeptService deptService;

    @Operation(summary = "获取部门树形列表")
    @GetMapping("/treeList")
    @PreAuthorize("hasAuthority('system:dept:list')")
    public Result<List<SysDept>> treeList(
            @Parameter(description = "部门名称") @RequestParam(required = false) String deptName,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        
        LambdaQueryWrapper<SysDept> wrapper = new LambdaQueryWrapper<SysDept>()
                .like(StringUtils.hasText(deptName), SysDept::getDeptName, deptName)
                .eq(status != null, SysDept::getStatus, status)
                .orderByAsc(SysDept::getSortOrder);
        
        List<SysDept> depts = deptService.list(wrapper);
        return Result.success(deptService.buildDeptTree(depts));
    }

    @Operation(summary = "获取部门详情")
    @GetMapping("/{deptId}")
    @PreAuthorize("hasAuthority('system:dept:query')")
    public Result<SysDept> getInfo(@PathVariable Long deptId) {
        return Result.success(deptService.getById(deptId));
    }

    @Operation(summary = "新增部门")
    @PostMapping
    @PreAuthorize("hasAuthority('system:dept:add')")
    public Result<Boolean> add(@RequestBody @Valid SysDept dept) {
        return Result.success(deptService.save(dept));
    }

    @Operation(summary = "修改部门")
    @PutMapping
    @PreAuthorize("hasAuthority('system:dept:edit')")
    public Result<Boolean> update(@RequestBody @Valid SysDept dept) {
        return Result.success(deptService.updateById(dept));
    }

    @Operation(summary = "删除部门")
    @DeleteMapping("/{deptId}")
    @PreAuthorize("hasAuthority('system:dept:delete')")
    public Result<Boolean> delete(@PathVariable Long deptId) {
        return Result.success(deptService.removeById(deptId));
    }

    @Operation(summary = "获取部门下拉树列表")
    @GetMapping("/treeSelect")
    public Result<List<SysDept>> treeSelect() {
        List<SysDept> depts = deptService.list(new LambdaQueryWrapper<SysDept>()
                .eq(SysDept::getStatus, 1)
                .orderByAsc(SysDept::getSortOrder));
        return Result.success(deptService.buildDeptTree(depts));
    }
} 
